﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using common;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class RepireControl : UserControl
    {
        Util util = new Util();
        int pagecount = 18;//每页数据行数
        RemindBI remindBI;
        MaintenBI maintenBI = new MaintenBI();
        RepireBI repireBI = new RepireBI();
        DataPaging RepirePage;
        DataSet RepireDataSet;
        public RepireControl()
        {
            InitializeComponent();
            RepirePage = repireBI.GetRepiredepage(pagecount);
            if (this.Btn_search.Image != null)
            {
                Btn_search.Image = new Bitmap(Btn_search.Image, Btn_search.Height - 4, Btn_search.Height - 4);
            }
            //查找所有分公司作为数据源
            RBAll.Checked = true;
            this.dateTimePicker1.Enabled = false;
            this.dateTimePicker2.Enabled = false;
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0] = "0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
            TBcompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBcompany.DataSource = companyTable;
            this.TBcompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBcompany.ValueMember = "ID";//标识
            RepireGridView.ReadOnly = true;
            initControl();

        }
        private void initControl()
        {
           // RepirePage = repireBI.GetRepiredepage(pagecount);
            RepireDataSet = RepirePage.Homepage();
            int x = maintenBI.GetDatacount(RepirePage);
            label1.Text = "总共" + maintenBI.GetDatacount(RepirePage) + "条记录，当前第" + RepirePage.GetPage() + "页，每页" + pagecount + "条";
            RepireGridView.DataSource = RepireDataSet;
            RepireGridView.DataMember = "data";
        }

        private void RepireControl_Load(object sender, EventArgs e)
        {

        }
        private void btn_mainpage_Click(object sender, EventArgs e)
        {
            initControl();
        }
        /*
       * 上一页
       * */
        private void btn_lastpage_Click(object sender, EventArgs e)
        {
            if (RepirePage.GetPage() > 1)
            {
                RepireDataSet = RepirePage.GetForeData();
                RepireGridView.DataSource = RepireDataSet;
                label1.Text = "总共" + maintenBI.GetDatacount(RepirePage) + "条记录，当前第" + RepirePage.GetPage() + "页，每页" + pagecount + "条";
                RepireGridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是第一页");
            }
        }
        /*
         * 下一页
         * */
        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            if (RepirePage.GetPage() < RepirePage.GetTotalCount())
            {
                RepireDataSet = RepirePage.GetFollowData();
                RepireGridView.DataSource = RepireDataSet;
                label1.Text = "总共" + maintenBI.GetDatacount(RepirePage) + "条记录，当前第" + RepirePage.GetPage() + "页，每页" + pagecount + "条";
                RepireGridView.DataMember = "data";
            }
            else
            {
                MessageBox.Show("已是最后一页");
            }
        }
        /*
         * 末页
         * */
        private void btn_endpage_Click(object sender, EventArgs e)
        {
            if (RepirePage.GetTotalCount() == 1)
            {
                initControl();
            }
            else
            {
                RepireDataSet = RepirePage.TailPage();
                if (RepireDataSet != null && RepireDataSet.Tables["data"].Rows.Count > 0)
                {
                    RepireGridView.DataSource = RepireDataSet;
                    label1.Text = "总共" + maintenBI.GetDatacount(RepirePage) + "条记录，当前第" + RepirePage.GetPage() + "页，每页" + pagecount + "条";
                    RepireGridView.DataMember = "data";
                }
            }
        }
        /*
         * 跳页
         * */
        private void btn_skippage_Click(object sender, EventArgs e)
        {
            if (util.isnumeric(textBoxX3.Text.Trim()))
            {
                int skippage = 1;
                if (textBoxX3.Text.Trim() != "" && textBoxX3.Text != null)
                {
                    skippage = Convert.ToInt32(textBoxX3.Text.Trim());
                }
                if (skippage == 1)
                {
                    initControl();
                }
                else
                {
                    RepireDataSet = RepirePage.SkipPage(skippage);
                    if (RepireDataSet != null && RepireDataSet.Tables["data"].Rows.Count > 0)
                    {
                        RepireGridView.DataSource = RepireDataSet;
                        label1.Text = "总共" + maintenBI.GetDatacount(RepirePage) + "条记录，当前第" + RepirePage.GetPage() + "页，每页" + pagecount + "条";
                        RepireGridView.DataMember = "data";
                    }
                }
            }
            else
                MessageBox.Show("请输入数字再进行跳页");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(RepireGridView, RepirePage.allData(),1);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
       
        }

        private void RepireGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (RepireGridView.CurrentRow != null && e.ColumnIndex >= 0)
            {
                if (RepireGridView.Columns[e.ColumnIndex].Name == "Column23" && RepireGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "放弃" && RepireGridView.Rows[e.RowIndex].Cells[e.ColumnIndex-1].Value.ToString() != "已升级")
                {
                    string id = RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString();
                    GiveupForm giveup = new GiveupForm(Convert.ToInt32(id));
                    giveup.ShowDialog();
                    if (giveup.DialogResult == DialogResult.Yes)
                    {
                        this.initControl();
                    }
                }
                else if (RepireGridView.Columns[e.ColumnIndex].Name == "Column22" && RepireGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "升级")
                {
                    int repireID = Convert.ToInt32(RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString());
                    int clientID = Convert.ToInt32(RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 2].Value.ToString());
                    string clientName = RepireGridView.CurrentRow.Cells["Column13"].Value.ToString();
                    FormAddMainten addmaintenform = new FormAddMainten(repireID, clientID, clientName);
                    addmaintenform.ShowDialog();
                    if (addmaintenform.DialogResult == DialogResult.Yes)
                    {
                        this.initControl();
                    }
                }
                else if (RepireGridView.Columns[e.ColumnIndex].Name == "Column2")
                {
                    int Ifhandle = Convert.ToInt32(RepireGridView.CurrentRow.Cells["Column4"].Value.ToString());
                    int repireID = Convert.ToInt32(RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString());
                    RepireDetailForm RepireForm = new RepireDetailForm(repireID,Ifhandle);
                    RepireForm.ShowDialog();
                    if (RepireForm.DialogResult == DialogResult.Yes)
                    {
                             this.initControl();
                    }
                }
            }
        }

        private void TBcompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
            RepirePage = repireBI.SearchBycompany(pagecount, fuzzytextbox.Text.Trim(), TBcompany.Text.Trim());
            initControl(); 
            
        }
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string begindate = dateTimePicker1.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string enddate = dateTimePicker2.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string companyname = "";
            
            if (TBcompany.SelectedValue != null)
            {
                companyname = TBcompany.Text.Trim();
            }

            if (this.dateTimePicker1.Value <= this.dateTimePicker2.Value)
            {
                RepirePage = repireBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyname, begindate, enddate);
                initControl();
            }
            else
            {
                MessageBox.Show("请重新选择时间段！");
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            string begindate = dateTimePicker1.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string enddate = dateTimePicker2.Text.Replace("年", "/").Replace("月", "/").Replace("日", "");
            string companyname = "";
        
            if (TBcompany.SelectedValue != null)
            {
                companyname = TBcompany.Text.Trim();
            }
          
            if (this.dateTimePicker1.Value <= this.dateTimePicker2.Value)
            {
                RepirePage = repireBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyname, begindate, enddate);
                initControl();
            }
            else
            {
                MessageBox.Show("请重新选择时间段！");
            }
        }
        private void RBAll_CheckedChanged(object sender, EventArgs e)
        {
            string begindate;
            string enddate;
            string companyname = "";
       
            if (TBcompany.Text.Trim()!="")
            {
                companyname = TBcompany.Text.Trim();
            }
           
            RadioButton thisRadioBtn = (RadioButton)sender;
            int tag = Convert.ToInt32(thisRadioBtn.Tag.ToString());
            switch (tag)
            {
                case 1:
                    //点击全部按钮
                    if (thisRadioBtn.Checked)
                    {
                         initControl();
                    }
                    break;

                case 2:
                    //点击近三天
                    if (thisRadioBtn.Checked)
                    {
                        // threeDays();
                        begindate = DateTime.Now.AddDays(-3).ToString("yyyy/M/d");
                        enddate = DateTime.Now.ToString("yyyy/M/d");
                       RepirePage = repireBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyname,  begindate, enddate);
                       initControl();
                    }
                    break;

                case 3:
                    //点击近一周
                    if (thisRadioBtn.Checked)
                    {
                        // aweek();
                        begindate = DateTime.Now.AddDays(-7).ToString("yyyy/M/d");
                        enddate = DateTime.Now.ToString("yyyy/M/d");
                        RepirePage = repireBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyname, begindate, enddate);
                        initControl(); 
                    }
                    break;

                case 4:
                    //点击近一月
                    if (thisRadioBtn.Checked)
                    {
                        //   amonth();
                        begindate = DateTime.Now.AddDays(-30).ToString("yyyy/M/d");
                        enddate = DateTime.Now.ToString("yyyy/M/d");
                       RepirePage = repireBI.Searchbytime(pagecount, fuzzytextbox.Text.Trim(), companyname,  begindate, enddate);
                       initControl();
                    }
                    break;

                case 5:
                    //点击自定义
                    if (thisRadioBtn.Checked)
                    {
                        this.dateTimePicker1.Enabled = true;
                        this.dateTimePicker2.Enabled = true;

                    }
                    else
                    {
                        this.dateTimePicker1.Enabled = false;
                        this.dateTimePicker2.Enabled = false;
                    }
                    break;
            }


        }

        private void Btn_search_Click(object sender, EventArgs e)
        {

            RepirePage = repireBI.fuzzysearch(pagecount, fuzzytextbox.Text.Trim());
            DataTable companyTable = maintenBI.Getallscompany();
            DataRow companydr = companyTable.NewRow();
            companydr[0] = "0";
            companydr[1] = "";
            companyTable.Rows.InsertAt(companydr, 0);
            //   TBcompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBcompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBcompany.DataSource = companyTable;
            this.TBcompany.DisplayMember = "ScompanyName";//客户所看到的
            this.TBcompany.ValueMember = "ID";//标识
            initControl();
        }
       
        private void Btn_addrepire_Click(object sender, EventArgs e)
        {
            AddRepireForm addRepireForm = new AddRepireForm();
            addRepireForm.ShowDialog();
            if (addRepireForm.DialogResult == DialogResult.Yes)
            {
                RepirePage = repireBI.GetRepiredepage(pagecount);
                this.initControl();
            }
        }
    }
}
